<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\api\model\User as UserModel;
use app\api\model\UserCar as UserCarModel;
use think\Db;
use think\Request;

class Usercar extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';
    protected $UserCarModel = null;
    public function _initialize()
    {
        parent::_initialize();
        $this->UserCarModel = new UserCarModel();
    }

    /**
     * 热门品牌
     */
    public function modelshot()
    {
        $Carhot = $this->UserCarModel->getHot();
        if ($Carhot)
        {
            $this->success('获取成功',$Carhot,200);
        }else{
            $this->success('目前没有数据','',200);
        }
    }

    /**
     * 车型列表
     */
    public function modelslist()
    {
        $id = $this->request->param('id',0);
        $name = $this->request->param('name','');
        $modelslist = $this->UserCarModel->Getmodels($id,$name);
        $this->success('获取成功',$modelslist,200);
    }
    /**
     *添加车型
     **/
    public function Usermodels()
    {
        $uid = $this->auth->id;
        $label = $this->request->param('label'); // 厂牌
        $brand = $this->request->param('brand'); // 车型
        $year  = $this->request->param('year'); //年份
        $engine= $this->request->param('engine'); // 排量
        $models= $this->request->param('models'); //款型

        if (empty($brand) || empty($year) || empty($label) || empty($models) || empty($engine))
        {
            $this->success('缺少车辆信息','',400);
        }
        $UserModel = new UserModel();
        $user = $UserModel->getUser($uid);
        if (!$user) $this->success('用户信息不存在请从新登录','',401);
        $UserCarModel = new UserCarModel();
        $data = [
            'uid'    => $user['id'],
            'name'   => $user['nickname'],
            'brand'  => $brand,
            'engine' => $engine,
            'models' => $models,
            'label'  => $label,
            'year'   => $year,
            'create_time'   => time(),
        ];
        $response = $UserCarModel->add($data);
        if ($response)
        {
            $this->success('添加成功',$response,200);
        }else{
            $this->success('添加失败','',400);
        }
    }

    /**
     * 车牌
     */
    public function plateList()
    {
        $Plate = $this->UserCarModel->getPlate();
        if ($Plate)
        {
            $this->success('查询成功',$Plate, 200);
        }else{
            $this->success('没有数据','', 200);
        }
    }

    /**
     * 添加车辆信息
     */
    public function vehicle()
    {
        $uid   = $this->auth->id;
        $cid   = $this->request->param('cid');
        $pid   = $this->request->param('pid');
        $plate = $this->request->param('plate');
        $frame = $this->request->param('frame');

        if (!$cid) $this->success('请添加车型','',400);
        if (!$pid) $this->success('请选择车牌前缀','',400);
        if (!$plate) $this->success('请输入车牌号','',400);
        if (!$frame) $this->success('请输入车架信息','',400);

        $Plate = $this->UserCarModel->addPlate($cid, $pid, $plate, $frame);

        if ($Plate['status'])
        {
            $this->success('添加成功',$Plate['data'],200);
        }else{
            $this->success($Plate['msg'],'',400);
        }

    }

    /**
     * 爱车列表
     */
    public function Carlist()
    {

        $uid = $this->auth->id;
        $response = $this->UserCarModel->getlist($uid);
        if ($response)
        {
            $this->success('获取成功',$response,200);
        }else{
            $this->success('没有添加爱车','',200);

        }
    }

    /**
     *  爱车详情
     */
    public function carDetails()
    {
        $uid = $this->auth->id;
        $id = $this->request->param('id');
        if (!$id) $this->success('选择查看信息','',200);
        $details = $this->UserCarModel->getCardetails($id, $uid);
        if ($details['status'])
        {
            $this->success($details['msg'],$details,200);
        }else{
            $this->success($details['msg'],'',200);
        }
    }

    /**
     * 设置默认车型
     */
    public function carDefault()
    {
        $uid = $this->auth->id;
        $id = $this->request->param('id');

        if (!$id) $this->success('缺少参数','',400);

        $response = $this->UserCarModel->upDefault($id, $uid);

        if ($response)
        {
            $this->success('操作成功',$response,200);
        }else{
            $this->success('操作失败','',400);
        }
    }

    /**
     * 删除爱车
     */
    public function Modelsdel()
    {
        $uid = $this->auth->id;
        $ids = $this->request->param('ids');
        if (!$ids) $this->success('请选择您想删除的车型','',400);
        $res = $this->UserCarModel->del($uid,$ids);

        if ($res)
        {
            $this->success('删除成功',$res,200);
        }else{
            $this->success('删除失败','',400);
        }
    }

    /**
     * 修改爱车信息
     */
    public function modelsUpdate()
    {
        $id = $this->request->param('id');
        $brand = $this->request->param('brand');
        $year  = $this->request->param('year');
        $label = $this->request->param('label');
        $models= $this->request->param('models');
        $engine= $this->request->param('engine');
        if (!$id) $this->success('缺少参数', '', 400);

        $data = [
            'brand' => $brand,
            'year' => $year,
            'label' => $label,
            'models' => $models,
            'engine' => $engine,
        ];
        $response = $this->UserCarModel->carupdate($id, $data);
        if ($response['status'])
        {
            $this->success('更新成功',$response['data'],200);
        }else{
            $this->success($response['msg'],'',400);
        }

    }

    /**
     * 修改车架信息
     */
    public function frameupdate()
    {
        $id    = $this->request->param('id');
        $frame = $this->request->param('frame');
        if (empty($id) || empty($frame))
        {
            $this->success('缺少参数','',400);
        }
        $information = Db::name('car_information')->where('id',$id)->find();
        if (!$information) $this->success('信息不存在','',400);

        $response = Db::name('car_information')->where('id',$id)->setField('frame_number',$frame);
        if ($response)
        {
            $this->success('更新成功',$response,200);
        }else{
            $this->success('更新失败','',200);
        }

    }






}